package org.shilong.Utility.Loader;

import java.util.List;

import org.shilong.Loader.ImageLoader;
import org.shilong.MContext.Json.JSonContext;
import org.shilong.MContext.WebRequest.WebRequestContext;
import org.shilong.Model.FriendModel;
import org.shilong.UI.R;
import org.shilong.UI.friend.ShowFriendDetailFrame;
import org.shilong.Utility.IntentUtility;
import org.shilong.Utility.SharedPreferenceUtility;
import org.shilong.Utility.ToastUtility;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class NewFriendListViewLoader {

	private ListView mListView;

	private String[] arr;

	private Activity mContext;
	private String wei_id;

	public NewFriendListViewLoader(ListView lv, Activity con, String wei_id) {

		this.wei_id = wei_id;

		this.mListView = lv;

		this.mContext = con;

		arr = SharedPreferenceUtility
				.getPersonInfoFromSharedPreference(mContext);

	}

	public void startLoader() {

		// mHandler.post(new Job(wei_id));

		MyJobTask task = new MyJobTask(wei_id);
		task.execute();
	}

	private MyAdapter adp = null;

	private class MyJobTask extends AsyncTask<String, String, String> {

		private String weiboid;

		public MyJobTask(String id) {

			this.weiboid = id;

		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			ToastUtility.showToast(mContext, "开始获取微博好友..");
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub

			

			String rlt = WebRequestContext.createInstance(
					WebRequestContext.TYPE_QUERY_WEIBO_FRIEND)
					.getWebRequestRlt(arr[2]);

			System.out.println("获取的好友json值是：" + rlt);
			List<FriendModel> mList = (List<FriendModel>) JSonContext
					.createInstance(JSonContext.TYPE_QUERY_WEIBO_FRIENDS)
					.getJsonRlt(rlt);

			adp = new MyAdapter(mList);

			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub

			mListView.setAdapter(adp);

		}

	}

	// adapter

	private class MyAdapter extends BaseAdapter {

		private List<FriendModel> mList;

		public MyAdapter(List<FriendModel> mList) {

			this.mList = mList;

		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mList.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return mList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub

			final FriendModel model = mList.get(arg0);

			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			View root = inflater.inflate(
					R.layout.inner_friend_listview_item_layout, null);

			final ImageView iv = (ImageView) root
					.findViewById(R.id.inner_show_header_img_imageview);


			ImageLoader loader = new ImageLoader();

			Drawable draw = loader.loadImage(model.getProfile_image_url(),
					new ImageLoader.ImageCallBack() {

						@Override
						public void setImageDrawable(Drawable imageDrawable) {
							// TODO Auto-generated method stub

							iv.setImageDrawable(imageDrawable);
						}
					});

			if (draw != null)
				iv.setImageDrawable(draw);

			TextView inner_friend_screen_name_textview = (TextView) root
					.findViewById(R.id.inner_friend_screen_name_textview);
			TextView inner_friend_location_textview = (TextView) root
					.findViewById(R.id.inner_friend_location_textview);
			TextView inner_friend_desc_textview = (TextView) root
					.findViewById(R.id.inner_friend_desc_textview);

			inner_friend_screen_name_textview.setText(model.getScreen_name());
			inner_friend_location_textview.setText(model.getLocation());
			// inner_friend_desc_textview.setText(model.getDescription());

			/* 点击item时间 */

			root.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub

					Bundle bun = new Bundle();

					bun.putString("image_url", model.getProfile_image_url());
					bun.putString("weibo_id", model.getWeibo_id());

					IntentUtility.goNextFrame(mContext,
							ShowFriendDetailFrame.class, bun);

				}
			});

			return root;

		}
	}
}
